package edu.zut.main.socket;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

public class URLDemo {

    public static void main(String[] args) {

        String url = "https://blog.csdn.net/pashanhu6402/article/details/96428887";
        down(url);
    }


    public static void down(String sourceUrl){

        try {
            //将网络资源进行对象实例化
            URL url = new URL(sourceUrl);
            //打开网络资源
            URLConnection connection = url.openConnection();
            //从连接上读取网络信息
            InputStream is = connection.getInputStream();
            FileOutputStream os = new FileOutputStream(new File("blog.html"));

            //将读取的信息保存到本地文件
            int len;
            byte[] buffer = new byte[521];
            while ((len=is.read(buffer)) != -1){
                os.write(buffer,0,len);
            }
            os.close();
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
